package local.sumw3.findmydevices.object.bean;

import java.util.ArrayList;
import java.util.List;

import local.sumw3.json.JSONArray;
import local.sumw3.json.JSONException;
import local.sumw3.json.JSONObject;


public class Device {

    private String id;

    private String deviceDisplayName;

    private String rawDeviceModel;

    private String deviceStatus;

    private boolean locationEnabled;

    private boolean isLocating;

    private Location location;

    private String batteryStatus;

    private double batteryLevel;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDeviceDisplayName() {
        return deviceDisplayName;
    }

    public void setDeviceDisplayName(String deviceDisplayName) {
        this.deviceDisplayName = deviceDisplayName;
    }

    public String getRawDeviceModel() {
        return rawDeviceModel;
    }

    public void setRawDeviceModel(String rawDeviceModel) {
        this.rawDeviceModel = rawDeviceModel;
    }

    public String getDeviceStatus() {
        return deviceStatus;
    }

    public void setDeviceStatus(String deviceStatus) {
        this.deviceStatus = deviceStatus;
    }

    public boolean isLocationEnabled() {
        return locationEnabled;
    }

    public void setLocationEnabled(boolean locationEnabled) {
        this.locationEnabled = locationEnabled;
    }

    public boolean isLocating() {
        return isLocating;
    }

    public void setLocating(boolean isLocating) {
        this.isLocating = isLocating;
    }

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    public String getBatteryStatus() {
        return batteryStatus;
    }

    public void setBatteryStatus(String batteryStatus) {
        this.batteryStatus = batteryStatus;
    }

    public double getBatteryLevel() {
        return batteryLevel;
    }

    public void setBatteryLevel(double batteryLevel) {
        this.batteryLevel = batteryLevel;
    }

    public static List<Device> parseFromJSON(JSONObject obj) throws JSONException {
        List<Device> deviceList = null;
        if (obj != null) {
            JSONArray jsonDeviceArray = obj.getJSONArray("content");
            if (jsonDeviceArray != null && jsonDeviceArray.length() > 0) {
                deviceList = new ArrayList<Device>();
                for (int i = 0; i < jsonDeviceArray.length(); i++) {
                    JSONObject jsonDevice = jsonDeviceArray.getJSONObject(i);
                    if (jsonDevice != null) {
                        Device device = new Device();
                        device.setId(jsonDevice.getString("id"));
                        device.setDeviceDisplayName(jsonDevice.getString("deviceDisplayName"));
                        device.setDeviceStatus(jsonDevice.getString("deviceStatus"));
                        device.setRawDeviceModel(jsonDevice.getString("rawDeviceModel"));
                        device.setBatteryLevel(jsonDevice.getDouble("batteryLevel"));
                        device.setBatteryStatus(jsonDevice.getString("batteryStatus"));
                        device.setLocating(jsonDevice.getBoolean("isLocating"));
                        device.setLocationEnabled(jsonDevice.getBoolean("locationEnabled"));
                        device.setLocation(Location.parseFromJSON(jsonDevice.getJSONObject("location")));
                        deviceList.add(device);
                    }
                }

            }
        }
        return deviceList;
    }

    public String toString() {
        return deviceDisplayName == null ? "" : deviceDisplayName;
    }
}
